<?php

namespace Common\Bundle\LibraryBundle\Controller;

use Common\Bundle\LibraryBundle\Entity\Theme;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Common\Bundle\LibraryBundle\Form\ThemeType;

/**
 * @Route("/theme")
 */
class ThemeController extends Controller{

    /**
     * @Route("/new/{idThemeParent}", options={"expose"=true}, defaults={"idTheme"=null})
     * @Route("/{idThemeParent}/{idTheme}/edit", options={"expose"=true})
     * @Template()
     */
    public function editAction($idThemeParent, $idTheme)
    {
        $themeParent = null;
        if($idThemeParent != 0)
        {
            $themeParent = $this->getThemeRepository()->find($idThemeParent);
        }

        if ($idTheme === null) {
            $theme = new Theme();
        } else {
            $theme = $this->getThemeRepository()->find($idTheme);
        }

        if (!$theme) {
            throw $this->createNotFoundException();
        }

        $em = $this->getDoctrine();
        $form = $this->createForm(new ThemeType($idThemeParent, $em), $theme);

        if ($this->getRequest()->isMethod("post")) {
            $form->bind($this->getRequest());
            if ($form->isValid()) {
                $theme->setNom(ucfirst(mb_strtolower($theme->getNom(), "utf-8")));
                $theme->setActif(true);
                $this->getDoctrine()->getManager()->persist($theme);
                $this->getDoctrine()->getManager()->flush();
                if($idTheme!=null)
                {
                    $this->get('session')->getFlashBag()->add('success', 'Thème modifié');
                }
                else
                {
                    $this->get('session')->getFlashBag()->add('success', 'Thème ajouté');
                }
                return $this->redirect($this->generateUrl("common_library_theme_index_1", array("idTheme" => $idThemeParent)));
            }
        }

        return $this->render('CommonLibraryBundle:Theme:edit.html.twig', array(
            'form' => $form->createView(),
            'idTheme' => $idTheme,
            'themeParent' => $themeParent
        ));
    }

    /**
     * @Route("/{idTheme}/desactive", options={"expose"=true})
     */
    public function desactiveAction($idTheme)
    {
        $theme = $this->getThemeRepository()->find($idTheme);
        $themeParent = $theme->getTheme();

        $em = $this->getDoctrine()->getManager();
        $theme->setActif(false);
        $em->persist($theme);
        $em->flush();
        $this->get('session')->getFlashBag()->add('success', 'Thème désactivé');
        if($themeParent != null)
        {
            return $this->redirect($this->generateUrl("common_library_theme_index_1", array("idTheme" => $themeParent->getId())));
        }
        else
        {
            return $this->redirect($this->generateUrl("common_library_theme_index_1", array("idTheme" => 0)));
        }

    }

    /**
     * @Route("/{idTheme}/active", options={"expose"=true})
     */
    public function activeAction($idTheme)
    {
        $theme = $this->getThemeRepository()->find($idTheme);
        $themeParent = $theme->getTheme();

        $em = $this->getDoctrine()->getManager();
        $theme->setActif(true);
        $em->persist($theme);
        $em->flush();
        $this->get('session')->getFlashBag()->add('success', 'Thème activé');
        if($themeParent != null)
        {
            return $this->redirect($this->generateUrl("common_library_theme_index_1", array("idTheme" => $themeParent->getId())));
        }
        else
        {
            return $this->redirect($this->generateUrl("common_library_theme_index_1", array("idTheme" => 0)));
        }

    }

    /**
     * @Route("/", options={"expose"=true}, defaults={"idTheme"=0})
     * @Route("/{idTheme}", options={"expose"=true})
     * @Template()
     */
    public function indexAction($idTheme)
    {
        $themes = $this->getThemeRepository()->findTheme($idTheme);

        $tabThemeActif = array();
        $tabThemeInactif = array();

        $tabQcmActif = array();
        $tabQcmInactif = array();

        foreach($themes as $theme)
        {
            if($theme->getActif() == true)
            {
                $tabThemeActif[]=$theme;
            }
            else
            {
                $tabThemeInactif[]=$theme;
            }
        }
        $chemin = $this->getChemin($idTheme);
        $ajoutTheme = $this->verifAjoutTheme($idTheme);

        if($idTheme!=0)
        {
            $qcms = $this->getQcmRepository()->findBy(array('theme' => $idTheme));
            foreach($qcms as $qcm)
            {
                if($qcm->getActif() == true)
                {
                    $tabQcmActif[]=$qcm;
                }
                else
                {
                    $tabQcmInactif[]=$qcm;
                }
            }
        }

        return array(
            "themesActif" => $tabThemeActif,
            "themesInactif" => $tabThemeInactif,
            "qcmsActif" => $tabQcmActif,
            "qcmsInactif" => $tabQcmInactif,
            "chemin" => $chemin,
            'ajoutTheme' => $ajoutTheme,
            'idTheme' => $idTheme
        );
    }

    public function getChemin($idTheme)
    {
        $chemin = '';
        if($idTheme != null && $idTheme != 0)
        {
            $theme = $this->getThemeRepository()->find($idTheme);
            if($theme->getTheme() != null)
            {
                $chemin = '<a href="'.$this->generateUrl("common_library_theme_index_1", array("idTheme" => $theme->getId())).'">'.$theme->getNom().'</a>';
                $themeParent = $this->getThemeRepository()->find($theme->getTheme()->getId());
                while($themeParent != null)
                {
                    $chemin = '<a href="'.$this->generateUrl("common_library_theme_index_1", array("idTheme" => $themeParent->getId())).'">'.$themeParent->getNom().'</a>
                    '.'<span class="glyphicon glyphicon-chevron-right chevron-violet"></span> '.$chemin;
                    $themeParent = $themeParent->getTheme();
                }
            }
            else
            {
                $chemin = '<a href="/eye/web/app_dev.php/theme/'.$theme->getId().'">'.$theme->getNom().'</a>';
            }

        }
        else
        {
            $chemin = 'Tous les thèmes';
        }

        return $chemin;
    }

    public function verifAjoutTheme($idTheme)
    {
        $ajoutTheme = true;
        if($idTheme != null && $idTheme != 0)
        {
            $theme = $this->getThemeRepository()->find($idTheme);
            if($theme->getTheme() != null)
            {
                $themeParent = $this->getThemeRepository()->find($theme->getTheme()->getId());
                $i=0;
                while($themeParent != null)
                {
                    $themeParent = $themeParent->getTheme();
                    $i++;
                }
                if($i == $this->container->getParameter('nb_niveau_theme'))
                {
                    $ajoutTheme = false;
                }
            }
        }

        return $ajoutTheme;
    }

    /**
     * @return \Common\Bundle\LibraryBundle\Entity\Repository\ThemeRepository
     */
    protected function getThemeRepository() {
        return $this->getDoctrine()->getRepository('CommonLibraryBundle:Theme');
    }

    /**
     * @return \Common\Bundle\LibraryBundle\Entity\Repository\QcmRepository
     */
    protected function getQcmRepository() {
        return $this->getDoctrine()->getRepository('CommonLibraryBundle:Qcm');
    }
}